//
//  UpdatePopUpWindowView.m
//  更新提醒
//
//  Created by wxp on 2025/3/7.
//

#import "UpdatePopUpWindowView.h"

@implementation UpdatePopUpWindowView

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT);
        self.leftTop = XY(0, 0);
        self.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.4];
        [self addSubView];
        self.userInteractionEnabled = YES;
    }
    return self;
}

- (void)addSubView
{
    [self addSubview:self.backImageView];
    [self.backImageView addSubview:self.closeBtn];
    [self.backImageView addSubview:self.labelTitle];
    [self.backImageView addSubview:self.labelSubTitle];
    [self.backImageView addSubview:self.contentScrollView];
    [self.contentScrollView addSubview:self.labelContent];
    [self.backImageView addSubview:self.updateBtn];
    [self.backImageView addSubview:self.cancelBtn];
    
    [self resetViewWithModel:[ModelBtn new]];
}

#pragma mark - lazy
- (UIImageView *)backImageView
{
    if (_backImageView == nil) {
        _backImageView = [UIImageView  new];
        _backImageView.userInteractionEnabled = true;
        _backImageView.image = [UIImage imageNamed:@"icon-update-pop-bg"];
        _backImageView.widthHeight = XY(SCREEN_WIDTH-2*LEFT_MARDIN, (SCREEN_WIDTH-2*LEFT_MARDIN)/1029*1365);
    }
    return  _backImageView;
}
- (UIButton *)closeBtn
{
    if (_closeBtn == nil) {
        _closeBtn = [UIButton  new];
        _closeBtn.widthHeight = XY(Ratio(24)+LEFT_MARDIN*2, Ratio(24)+LEFT_MARDIN*2);
        [_closeBtn setImage:[UIImage imageNamed:@"icon-update-close"] forState: UIControlStateNormal];
        _closeBtn.tag = 1;
        [_closeBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _closeBtn;
}
- (UILabel *)labelTitle
{
    if (_labelTitle == nil) {
        _labelTitle = [UILabel  new];
        [GlobalMethod setLabel:_labelTitle widthLimit:0 isBold:true fontNum:16 textColor:COLOR_BLACK text:@"Actualización de la versión"];
    }
    return  _labelTitle;
}
- (UIScrollView *)contentScrollView
{
    if (_contentScrollView == nil) {
        _contentScrollView = [UIScrollView  new];
        _contentScrollView.showsVerticalScrollIndicator = false;
        _contentScrollView.backgroundColor = [UIColor clearColor];
    }
    return  _contentScrollView;
}
- (UILabel *)labelSubTitle
{
    if (_labelSubTitle == nil) {
        _labelSubTitle = [UILabel  new];
        [GlobalMethod setLabel:_labelSubTitle widthLimit:0 fontNum:14 textColor:COLOR_BLACK text:@"Actualización del contenido"];
    }
    return  _labelSubTitle;
}
- (UILabel *)labelContent
{
    if (_labelContent == nil) {
        _labelContent = [UILabel  new];
        [GlobalMethod setLabel:_labelContent widthLimit:0 fontNum:(14) textColor:COLOR_CONTENT text:@""];
        _labelContent.numberOfLines = 0;
        _labelContent.lineSpace = Ratio(3);
    }
    return  _labelContent;
}
- (UIButton *)updateBtn
{
    if (_updateBtn == nil) {
        _updateBtn = [UIButton  new];
        _updateBtn.backgroundColor = COLOR_PRIMARY;
        _updateBtn.titleLabel.font = [UIFont systemFontOfSize:(14)];
        [_updateBtn setTitle:@"Actualizar ahora" forState:UIControlStateNormal];
        [_updateBtn setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        _updateBtn.tag = 3;
        [_updateBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _updateBtn;
}
- (UIButton *)cancelBtn
{
    if (_cancelBtn == nil) {
        _cancelBtn = [UIButton  new];
        _cancelBtn.backgroundColor = COLOR_WHITE;
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:(14)];
        [_cancelBtn setTitle:@"Ninguna actualización por ahora" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:COLOR_LIGHT_GRAY forState:UIControlStateNormal];
        _cancelBtn.tag = 2;
        [_cancelBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _cancelBtn;
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    switch (sender.tag) {
        case 1:
        case 2:
        {
            [self removeFromSuperview];
        }
            break;
        case 3:
        {
            // 更新
            [self removeFromSuperview];
            if(self.model.isSelected == true){
                // true则跳转浏览器，false则走跳转谷歌商店逻辑
                // 要打开的 URL
                NSString *urlString = self.model.title;
                NSURL *url = [NSURL URLWithString:urlString];

                if ([[UIApplication sharedApplication] canOpenURL:url]) {
                    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
                } else {
                    // NSLog(@"不能打开这个 URL");
                }
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 刷新View
- (void)resetViewWithModel:(ModelBtn *)model{
    self.model = model;
    self.backImageView.centerXCenterY = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT/2-50);
    self.closeBtn.rightTop = XY(self.backImageView.width, Ratio(85));
    self.labelTitle.rightTop = XY(self.backImageView.width-LEFT_MARDIN-5, self.closeBtn.bottom-9);
    self.labelSubTitle.leftTop = XY(Ratio(30), self.labelTitle.bottom+Ratio(52));
    
    self.cancelBtn.widthHeight = XY(self.backImageView.width-30*2, BUTTON_HEIGHT);
    [GlobalMethod setRoundView:self.cancelBtn color:COLOR_LIGHT_GRAY numRound:BUTTON_HEIGHT/2 width:1];
    self.cancelBtn.leftBottom = XY(30, self.backImageView.height-(25));
    self.updateBtn.widthHeight = self.cancelBtn.widthHeight;
    [GlobalMethod setRoundView:self.updateBtn color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
    self.updateBtn.leftBottom = XY(30, self.cancelBtn.top-15);
    
    self.contentScrollView.widthHeight = XY(self.backImageView.width, self.updateBtn.top-Ratio(15)-self.labelSubTitle.bottom-Ratio(18));
    self.contentScrollView.leftTop = XY(0, self.labelSubTitle.bottom+Ratio(18));
    
    //[self.labelContent fitTitle:UnPackStr(model.content) variable:self.contentScrollView.width-2*self.labelSubTitle.left];
    // 显示富文本内容
    self.labelContent.width = self.contentScrollView.width-2*self.labelSubTitle.left;
    NSData *data = [readStr(model.content) dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding) };
    NSError *error;
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:&error];
    NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
    // 设置字体大小
    UIFont *font = [UIFont systemFontOfSize:14];  // 设置你想要的字体大小
    [mutableAttributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, mutableAttributedString.length)];
    // 设置行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = Ratio(3);  // 设置行间距
    [mutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, mutableAttributedString.length)];
    
    self.labelContent.attributedText = mutableAttributedString;
    self.labelContent.lineBreakMode = NSLineBreakByWordWrapping;
    [self.labelContent sizeToFit];
    self.labelContent.leftTop = XY(self.labelSubTitle.left, 0);
    
    self.contentScrollView.contentSize = CGSizeMake(0, self.labelContent.height);
}

@end
